package com.wirelesshomecontrol.gui.windows;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.google.gwt.dom.client.Style.Unit;
import com.vaadin.data.util.QueryContainer;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;
import com.wirelesshomecontrol.db.jdbcconnect;

public class HistoryWindow {

	Window historyWindow = new Window();
	private jdbcconnect cn = new jdbcconnect();
	public Table tblDataTable = null;
	public Table tblDataTable2 = null;

	public HistoryWindow() {
		initHistoryWindow();
	}

	private void initHistoryWindow() {
		historyWindow.setCaption("History");
		historyWindow.center();
		historyWindow.setWidth(800, Sizeable.UNITS_PIXELS);
		historyWindow.setHeight(600, Sizeable.UNITS_PIXELS);

		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(cn.dbUrl, "root","Hippos44");
			cn.qcSQLHistory = new QueryContainer("SELECT timestamp, message FROM log", con);
			tblDataTable = new Table();
			tblDataTable.setWidth(90, Sizeable.UNITS_PERCENTAGE);
			tblDataTable.setContainerDataSource(cn.qcSQLHistory);

			tblDataTable.setVisible(true);
			tblDataTable.setEnabled(true);
			tblDataTable.setCaption("LOGIN History");
			historyWindow.addComponent(tblDataTable);
			// cn.qcSQLHistory.close(); // Closes Query Can Not reopen

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		historyWindow.addComponent(new Label("<br/>",Label.CONTENT_XHTML));
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(cn.dbUrl, "root","Hippos44");
			cn.qcSQLSensor = new QueryContainer("SELECT nickname, status, timestamp FROM sensor_history", con);
			tblDataTable2 = new Table();
			tblDataTable2.setWidth(90, Sizeable.UNITS_PERCENTAGE);
			tblDataTable2.setContainerDataSource(cn.qcSQLSensor);
 
			tblDataTable2.setVisible(true);
			tblDataTable2.setEnabled(true);
			tblDataTable2.setCaption("SENSOR History");
			historyWindow.addComponent(tblDataTable2);
			// cn.qcSQLHistory.close(); // Closes Query Can Not reopen

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Window getHistory() {
		return historyWindow;
	}

	public void setHisotryWindow(Window historyWindow) {
		this.historyWindow = historyWindow;
	}
}
